<template>
    <div class="comment-section">
        <h3 class="section-title">评论</h3>

        <div class="comment-list">
            <div v-for="(comment, index) in comments" :key="comment.id" class="comment-item">
                <el-avatar :size="50" :src="comment.avatar">
                    {{ comment.nickname.charAt(0) }}
                </el-avatar>

                <div class="comment-content">
                    <div class="comment-header">
                        <span class="comment-nickname">{{ comment.nickname }}</span>
                        <span class="comment-time">{{ comment.time }}</span>
                        <el-button v-if="comment.userId === currentUserId" type="danger" size="small" text
                            @click="deleteComment(index)">
                            删除
                        </el-button>
                    </div>

                    <div class="comment-text">{{ comment.content }}</div>

                    <div class="comment-footer">
                        <el-button type="text" @click="toggleLike(comment.id)">
                            <el-icon :color="comment.isLiked ? '#f56c6c' : ''">
                                <StarFilled />
                            </el-icon>
                            {{ comment.likes }}
                        </el-button>
                    </div>
                </div>
            </div>
        </div>

        <div class="comment-input">
            <el-input v-model="newComment" :rows="3" type="textarea" placeholder="留下您的评论..." resize="none" />
            <div class="input-footer">
                <el-button type="primary" @click="handleSubmit" :disabled="!newComment.trim()">
                    发表评论
                </el-button>
            </div>
        </div>
    </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { StarFilled } from '@element-plus/icons-vue'

interface Comment {
    id: number
    userId: number
    nickname: string
    avatar?: string
    content: string
    time: string
    likes: number
    isLiked: boolean
}

const props = defineProps<{
    personId: number
}>()

const emit = defineEmits(['submit-comment'])

const newComment = ref('')
const currentUserId = ref(1) // 模拟当前用户ID

// 模拟评论数据
const comments = ref<Comment[]>([
    {
        id: 1,
        userId: 1,
        nickname: '张家族人',
        avatar: '',
        content: '这位先祖是我们家族的骄傲，他的事迹值得我们永远铭记！',
        time: '2023-05-10 14:30',
        likes: 12,
        isLiked: false
    },
    {
        id: 2,
        userId: 2,
        nickname: '李家族人',
        avatar: '',
        content: '感谢分享这些珍贵的历史资料，让我们更了解家族历史。',
        time: '2023-05-11 09:15',
        likes: 8,
        isLiked: true
    },
    {
        id: 3,
        userId: 2,
        nickname: '李家族人',
        avatar: '',
        content: '感谢分享这些珍贵的历史资料，让我们更了解家族历史。',
        time: '2023-05-11 09:15',
        likes: 5,
        isLiked: true
    }
])

const handleSubmit = () => {
    if (!newComment.value.trim()) return

    const comment = {
        id: Date.now(),
        userId: currentUserId.value,
        nickname: '当前用户',
        content: newComment.value,
        time: new Date().toLocaleString(),
        likes: 0,
        isLiked: false
    }

    comments.value.unshift(comment)
    emit('submit-comment', {
        personId: props.personId,
        comment: newComment.value
    })
    newComment.value = ''
}

const deleteComment = (index: number) => {
    comments.value.splice(index, 1)
}

const toggleLike = (commentId: number) => {
    const comment = comments.value.find(c => c.id === commentId)
    if (comment) {
        comment.isLiked = !comment.isLiked
        comment.likes += comment.isLiked ? 1 : -1
    }
}
</script>

<style scoped lang="scss">
.comment-section {
    background-color: white;
    border-top: 1px solid var(--el-border-color);
    padding: 14px;
    margin: 14px 2px;
    border-radius: 10px;

    .section-title {
        font-size: 22px;
        color: var(--el-color-primary);
        margin-bottom: 20px;
    }

    .comment-list {
        max-height: 300px;
        overflow-y: auto;
        margin-bottom: 20px;

        .comment-item {
            display: flex;
            gap: 15px;
            padding: 15px 4px;
            border-bottom: 1px dashed var(--el-border-color);
            border-radius: 8px;

            .el-avatar {
                background-color: var(--el-color-primary-light-8);
                color: var(--el-color-primary);
                flex-shrink: 0;
            }

            .comment-content {
                flex: 1;

                .comment-header {
                    display: flex;
                    align-items: center;
                    margin-bottom: 8px;

                    .comment-nickname {
                        font-weight: bold;
                        margin-right: 10px;
                    }

                    .comment-time {
                        font-size: 12px;
                        color: var(--el-text-color-secondary);
                        margin-right: auto;
                    }
                }

                .comment-text {
                    line-height: 1.5;
                    margin-bottom: 8px;
                }

                .comment-footer {
                    text-align: right;
                }
            }

            &:hover {
                transform: translateY(-3px);
                box-shadow: 0 6px 16px rgba(0, 0, 0, 0.1);
                background-color: #f9f5f0;
                transition: all 0.3s ease;
                border-radius: 8px;
                border: 1px solid var(--border-color);
            }
        }
    }

    .comment-input {
        .input-footer {
            display: flex;
            justify-content: flex-end;
            margin-top: 15px;
        }
    }
}
</style>